package com.soft.controller;

import com.github.pagehelper.PageInfo;
import com.soft.entity.TUser;
import com.soft.entity.User;
import com.soft.service.TUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * (TUser)表控制层
 *
 * @author makejava
 * @since 2021-12-22 19:00:30
 */
@Controller
@RequestMapping("/tUser")
public class TUserController {
    /**
     * 服务对象
     */
    @Resource
    private TUserService tUserService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public TUser selectOne(Integer id) {
        return this.tUserService.queryById(id);
    }

    @RequestMapping("users")
//    @ResponseBody
    public String getAllUser(int offset, int limit, Model model){
        PageInfo<TUser> tUsers = tUserService.findAll(offset, limit);
        model.addAttribute("userList", tUsers);
        return "admin/user";
    }

    @GetMapping("findUserByName")
    @ResponseBody
    public String findUserByName(String name,Model model){
        if (tUserService.queryByNameForBoolean(name)) {
            return "ok";
        }return "not";
    }
    @GetMapping("insertUser")
    @ResponseBody
    public String insertUser(String name, String pwd,Model model){
        Date date = new Date(System.currentTimeMillis());
        TUser tUser = new TUser();
        tUser.setUpdateTime(date);
        tUser.setCreateTime(date);
        tUser.setPassword(pwd);
        tUser.setUsername(name);
        if ( tUserService.insert(tUser).getId() != null){
            return "ok";
        }else return "not";
    }

}